_arry = _this select 0;
_payload = _this select 1;
_payloadtype = _this select 2;
//hint format ["%1",_this];

_helo = _arry select 0;
_callsign = _arry select 1;
_home = _arry select 2;
_airport = _arry select 3;
_egress = _arry select 4;
_ingress = _arry select 5;

_paycallsign = _payload select 0;
_class = _payload select 1;
_init = _payload select 2;

if(_helo getVariable "RUM_inFlight")exitwith{hint "Helo in flight";};

/////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// Start of Map section //////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////

RUM_ASclick = false;

openMap [true,false];

for [{_n=1},{_n<=3},{_n=_n+1}] do {
	_mkrname = "";
	_type = "";
	_hint = "";
	switch (_n) do {
		case 1: {
			_mkrname = format ["%1target",_helo];
			_type = "mil_pickup";
			_hint = "Select Drop Zone";
		};
		case 2: {
			_mkrname = format ["%1ingress",_helo];
			_type = "mil_start";
			_hint = "Select Drop Zone Ingress Point";
		};
		case 3: {
			_mkrname = format ["%1egress",_helo];
			_type = "mil_end";
			_hint = "Select Drop Zone Egress Point";
		};
		default {
			hint "shit";	
		};
	};
	_asloc = createMarkerLocal [_mkrname, [0,0,0]];
	_asloc setMarkerShapeLocal "ICON";
	_mkrname setMarkerTypeLocal _type;
	_stgtar = format ["'%1' setMarkerPosLocal _pos;RUM_ASclick = true",_mkrname];
	onMapSingleClick _stgtar;
	titleText [_hint, "PLAIN DOWN"];
	waitUntil {RUM_ASclick};
	if((_n == 2) or (_n == 3)) then {
		_mkrname2 = format ["%1target",_helo];
		_mkrname setMarkerDirLocal ((getMarkerPos _mkrname2 select 0)-(getMarkerPos _mkrname select 0)) atan2 ((getMarkerPos _mkrname2 select 1)-(getMarkerPos _mkrname select 1));
	};
	onMapSingleClick "";
	RUM_ASclick = false;
};
titleFadeOut 0;
sleep 1;
openMap [false,false];

/////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// End of Map section //////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// Start of Helo Setup //////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////

_helo setVariable ["RUM_inFlight", true, true];

_crew = [];
_grp = createGroup side player;
_pilot = _grp createUnit [RUM_SUP_Pilot, getpos _home, [], 0, "FORM"];
_helo lockDriver false;
_pilot moveinDriver _helo;
_helo lockDriver true;

_crew = _crew + [_pilot];

_grp2 = createGroup side player;
_cfg=configFile >> "CfgVehicles" >> typeof _helo >> "turrets";
for [{_n=0},{_n<=(count _cfg)},{_n=_n+1}] do {
	_gunner = _grp2 createUnit [RUM_SUP_Pilot, getpos _home, [], 0, "FORM"];
	_helo lockTurret [[_n], false];
	_gunner moveinTurret [_helo,[_n]];
	_helo lockTurret [[_n], true];
	if!(_gunner in crew _helo)then{deleteVehicle _gunner};
	_crew = _crew + [_gunner];
};

_grp2 setCombatMode "RED";
_grp2 setBehaviour "COMBAT";

/////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// End of Helo Setup //////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// Start of Supply ///////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////

_height1=100;
if (_helo isKindOf "Helicopter") then {_height1 = RUM_SUP_flyHeight} else {_height1 = RUM_SUP_flyHeightPLN};
_helo flyInHeight _height1;
_height2=RUM_SUP_flyDropHeight;

if(_payloadtype == "Sling Load")then{
	_dropitem = createVehicle [_class, [0,0,0], [], 0, "NONE"];

	_dropitem setVehicleInit _init;
	processInitCommands;
	[_helo,_dropitem] execVM (RUM_PATH+"Supply\Sling.sqf");
	["sling",[player,_pilot,_callsign,_paycallsign]] execVM (RUM_PATH+"Supply\radiodialogs.sqf");
} else {
	["para",[player,_pilot,_callsign,_paycallsign]] execVM (RUM_PATH+"Supply\radiodialogs.sqf");
};

sleep 14;

_helo engineOn true;

for [{_n=1},{_n<=3},{_n=_n+1}] do {
	switch (_n) do {
		case 1: {
			[_mkrname,_grp,_egress] call RUM_THel_moveWPTakeoff; 
			sleep 15;
			[_mkrname,_grp,_egress] call RUM_SUP_movewaypoint;
		};
		case 2: {
			_mkrname = format ["%1ingress",_helo];
			[_mkrname,_grp] call RUM_SUP_movewaypoint;
		};
		case 3: {
			_mkrname = format ["%1target",_helo];
			_dz = [_mkrname,_grp] call RUM_SUP_landwaypoint;
		};
	};
};

if(_payloadtype == "Para Drop")then{
	_mkrname = format ["%1egress",_helo];
	[_mkrname,_grp] call RUM_SUP_movewaypoint;
};

_cont = format ["%1",_helo];
RUM_SUP_land = "";
waitUntil {(!alive _helo) or (RUM_SUP_land == _cont)};

if(_payloadtype == "Sling Load")then{

	_height = 4.8;
	_helo flyInHeight _height;
	waitUntil {(!alive _helo) or (getposATL _helo select 2 <= _height) or (getposASL _helo select 2 <= _height)};
	detach _dropitem;
	_dropitem setPos [getpos _dropitem select 0, getpos _dropitem select 1,0];
	_helo setvariable ["lift",false,true];
	_helo FlyInHeight RUM_SUP_flyHeight;
	
}else{

	_heloDropPos = getPos _helo;
	_heloDropDir = getDir _helo;
	_droppos = [(_heloDropPos select 0) - (12 * sin(_heloDropDir)), (_heloDropPos select 1) - (12 * cos(_heloDropDir)), (_heloDropPos select 2)];
	_dropitem = createVehicle [_class, _droppos, [], 0, "NONE"];

	_dropitem setVehicleInit _init;
	processInitCommands;
	
	_chute = "ParachuteMediumWest" createVehicle _droppos;
	_chute setPos _droppos;
	
	// create smoke or IR on chute for tracking while in flight - IR will be visable on the ground as well.
	private ["_marker","_markType"];
	
	_dayTime = daytime;
	
	if ((_dayTime < 19) && (_dayTime > 7)) then {
		_markType = "SmokeShellBlue";
	} else {
		_markType = "NVG_TargetC";
	};

	_marker =  _markType createVehicle position _dropitem;
	_marker setPosATL (getPosATL _dropitem);
	_marker attachTo [_dropitem,[0,0,1]];
	

	_dropitem attachTo [_chute,[0,0,0]];
	_chute setVelocity velocity _helo;
	if(_helo iskindof "Plane")then{
		_helo FlyInHeight RUM_SUP_flyHeightPLN;
	}else{
		_helo FlyInHeight RUM_SUP_flyHeight;
	};
	[_dropitem, _chute, _marker] spawn {
		_dropitem = _this select 0;
		_chute = _this select 1;
		_marker = _this select 2;
		
		// Waiting till on ground
		waitUntil {isNull _chute};
		// waitUntil {sleep 2; (getPosATL _dropitem select 2) < 3};
		detach _dropitem;
		detach _marker;
		sleep 0.3;
		_dropitem setPosATL [(getPosATL _dropitem select 0),(getPosATL _dropitem select 1),0.1];
		_marker setPosATL [(getPosATL _dropitem select 0) + 1,(getPosATL _dropitem select 1) + 1,0.1];

		// adds green smoke for when the object has landed
		if(RUM_SUP_smoke)then{"SmokeShellGreen" createVehicle (getpos _dropitem)};
		
		// Enable R3F compatibility
		_dropitem setVariable ["R3F_LOG_disabled", false];
		
		// Enable PDB saving for MSO
		_id = 1000 + ceil(random(9000));
		_name = format["RUM_log_%1",_id];
		_dropitem setVariable ["pdb_save_name", _name, true];
	};
};

for [{_n=1},{_n<=3},{_n=_n+1}] do {
	_mkrname = "";	
	switch (_n) do {	
		case 1: {
			_mkrname = format ["%1egress",_helo];
			[_mkrname,_grp] call RUM_THel_moveWPLZDustOff; 
			[_mkrname,_grp] call RUM_THel_moveWPNormal;
		};
		case 2: {
			[_mkrname,_grp,_ingress] call RUM_THel_movewaypoint;
			hintsilent format ["%1",_ingress];
		};
		case 3: {
			[_mkrname,_grp,_home] call RUM_THel_LandHome;
			sleep 5;
			hintsilent format ["%1",_home];
		};
	};
};
	_end = format ["%1",_helo];
	sleep 5;
	waitUntil{(!alive _helo) OR (RUM_THel_endflight == _end)};

	// crew members will exit vehicle
	{unassignVehicle _x;_x action ["GETOUT",_helo];} forEach units _grp2;
	// wait till player(s) have exited the vehicle
	waitUntil{(!alive _helo) OR (count(crew _helo) == 0)};

// check to make sure the vehicle is alive and that it has no crew members still mounted
if ((alive _helo) and (count(crew _helo) == 0)) then {
	sleep 1;
	// delete all crew members
	{deleteVehicle _x} forEach _crew;
	// give the helo time to settle before it is repositioned and refitted.
	sleep 15;
	_helo setposASL getposASL _home;
	_helo setdir getdir _home;
	_helo setDamage 0;
	_helo setFuel 1;
	_mags = getArray (configFile>>"cfgVehicles">>typeOf _helo>>"Turrets">>"MainTurret">>"magazines");
	{_helo removeMagazine _x} forEach magazines _helo;
	{_helo addMagazine _x} forEach _mags;
};

// delete all the markers that were created for this instance.
_mkrname1 = format ["%1ingress",_helo];
_mkrname2 = format ["%1target",_helo];
_mkrname3 = format ["%1egress",_helo];
deletemarkerLocal _mkrname1;
deletemarkerLocal _mkrname2;
deletemarkerLocal _mkrname3;

// this will add the vehicle back to the RUM PDA when resupply has finished. (still has an issue with var passed back)
// player sidechat format["%1",_arry];

sleep RUM_SUP_ReSupTime;

RUM_SUPAircraft = RUM_SUPAircraft + [_arry];
_helo setVariable ["RUM_inFlight", false, true];

/////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////// End of Supply //////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////

// exit script
if(true)exitwith{};